//
// perc2 - calculation of the fractal dimension of correlated
// bond percolation cluster hulls
//
// Copyright (C) 2009, 2010 Indrek Mandre <indrek(at)mare.ee>
// http://www.mare.ee/indrek/perc2/, http://code.google.com/p/perc2/
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// 

#include "common.cpp"

static void usage (const char *argv0)
{
  fprintf (stderr, "%s: %s [-e exponent] [-r seed] [-R rnd.bin] [-o printsurf.csv] [-g generator] [-b] [-x xsize ysize] [-d] [-l growthpow startlayer endlayer]\n", argv0, argv0);
  fprintf (stderr, "    -e exponent - set the correlation exponent\n");
  fprintf (stderr, "    -r seed - set the random seed\n");
  fprintf (stderr, "    -R rnd.bin - random binary bitstream data\n");
  fprintf (stderr, "    -o printsurf.csv - output data to file\n");
  fprintf (stderr, "    -g generator - use the given random generator, put 'list' to see available\n");
  fprintf (stderr, "    -b - add baseline\n");
  fprintf (stderr, "    -x - field size to print\n");
  fprintf (stderr, "    -d - print a discrete boolean field\n");
  fprintf (stderr, "    -l growthpow startlayer endlayer - specify the layer growth base and layer range\n");
  exit(-1);
}

int main (int argc, char *argv[])
{
  const char *outfn = "printsurf.csv";
  int xsize = 256;
  int ysize = 256;
  int rnd_seed = 0;
  EXPONENT = 0.75;
  bool add_baseline = false;
  bool print_boolean = false;
  const char *rndpath = 0;

  const gsl_rng_type * rng_type = gsl_rng_default;

  for ( int i = 1; i < argc; i++ )
    {
      if ( argv[i][0] != '-' || argv[i][1] == 0 || argv[i][2] != 0 )
          usage (argv[0]);

      switch (argv[i][1])
        {
          case 'x': {
            if ( i + 2 >= argc )
                usage(argv[0]);
            if ( sscanf (argv[++i], "%d", &xsize) != 1 ||
                sscanf (argv[++i], "%d", &ysize) != 1 ||
                xsize < 1 || ysize < 1 )
                usage (argv[0]);
            break;
          }
          case 'l': {
            if ( i++ == argc || sscanf (argv[i], "%lf", &BASEPOW) != 1 ||
                i++ == argc || sscanf (argv[i], "%d", &LAYERSTART) != 1 ||
                i++ == argc || sscanf (argv[i], "%d", &LAYEREND) != 1 )
                usage (argv[0]);
            break;
          }
          case 'd': {
            print_boolean = true;
            break;
          }
          case 'b': {
            add_baseline = true;
            break;
          }
          case 'o': {
            i++;
            if ( i == argc )
                usage (argv[0]);
            outfn = argv[i];
            break;
          }
          case 'r': {
            i++;
            if ( i == argc || sscanf (argv[i], "%d", &rnd_seed) != 1 )
                usage (argv[0]);
            break;
          }
          case 'R': {
            i++;
            if ( i == argc )
                usage (argv[0]);
            rndpath = argv[i];
            break;
          }
          case 'e': {
            i++;
            if ( i == argc || sscanf (argv[i], "%lf", &EXPONENT) != 1 )
                usage (argv[0]);
            break;
          }
          case 'g': {
            i++;
            if ( i == argc )
                usage (argv[0]);
            rng_type = 0;
            for ( const gsl_rng_type **t = gsl_rng_types_setup(); *t != 0; t++ )
              {
                if ( strcmp ((*t)->name, argv[i]) == 0 )
                  {
                    rng_type = *t;
                    break;
                  }
              }
            if ( !rng_type )
              {
                printf ("Random generators available:\n");
                for ( const gsl_rng_type **t = gsl_rng_types_setup(); *t != 0; t++ )
                    printf ("  %s\n", (*t)->name);
                exit(0);
              }
            break;
          }
          default: {
            usage (argv[0]);
            break;
          }
        }
    }

  if ( xsize > FIELD_SIZE || ysize > FIELD_SIZE )
    {
      fprintf (stderr, "Error: pritn size too large for the compile time field size!\n");
      abort();
    }

  rndf.seed (rng_type, rnd_seed, rndpath);

#ifdef UNCORRELATED
  printf ("Uncorrelated, ignoring the correlation exponent\n");
#else
  printf ("Correlation exponent is %f\n", EXPONENT);
  printf ("Cumulation %d\n", CUMULATION);
  printf ("Layer size growth power %g\n", (double)BASEPOW);
  printf ("Adding baseline: %s\n", add_baseline ? "YES" : "NO");
#endif
  printf ("Field size %d\n", FIELD_SIZE);

  Field *f = new Field(add_baseline);

  f->printsurf(outfn, xsize, ysize, print_boolean);

  int subsample = 10;

  int y = ysize / 2;
  FILE *fout = fopen ("printrel.csv", "w");
  for ( int i = 0; i <= xsize; i++ )
    {
      fprintf (fout, "%g %g\n", (double)i, v2d(f->value (i, y)));
      if ( i != xsize )
        {
          for ( int j = 0; j < subsample; j++ )
            {
              double x = i + (j + 1) * 1.0 / (subsample + 1);
              fprintf (fout, "%g %g\n", x, v2d(f->value (x, y)));
            }
        }
    }
  fclose (fout);

  delete f;

  return 0;
}

